package j20251108;

public class Solution1 {
    boolean[][] vis;
    int m,n,count;
    int[] dx={0,0,-1,1};
    int[] dy={1,-1,0,0};
    public int maxAreaOfIsland(int[][] grid) {
        int ret=0;
        m=grid.length;
        n=grid[0].length;
        vis=new boolean[m][n];
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(!vis[i][j]&&grid[i][j]==1){
                    count=0;
                    dfs(grid,i,j);
                    ret=Math.max(ret,count);
                }
            }
        }
        return ret;
    }
    public void dfs(int[][] g,int i,int j){
        count++;
        vis[i][j]=true;
        for(int k=0;k<4;k++){
            int x=i+dx[k];
            int y=j+dy[k];
            if(x>=0&&x<m&&y>=0&&y<n&&!vis[x][y]&&g[x][y]==1){
                dfs(g,x,y);
            }
        }
    }
}
